import time
import requests
from perf_settings import *
from common.hande_device import HandleDevices
from influxdb_client import Point

from common.handle_influxdb import InfluxDBHandler

do_devices = HandleDevices(CPDemo_pkgName=CPDemo_pkgName, SDKServer_pkgName=SDKServer_pkgName)
do_influxdb = InfluxDBHandler()

apm_pid_main_cpdemo, apm_pid_pid_att_cpdemo, apm_pid_main_server = do_devices.handle_main_device(
    deviceId_001=deviceId_001)
if Secondary_device:
    apm_pid_sec_cpdemo, apm_pid_sec_server = do_devices.handle_sec_device(deviceId_002=deviceId_002)

while True:
    # 准备数据点列表
    try:
        time.sleep(2)
        device_001_cpu_main_cpdemo = do_devices.get_cpu(apm_pid_main_cpdemo)
        device_001_cpu_att_cpdemo = do_devices.get_cpu(apm_pid_pid_att_cpdemo)
        device_001_cpu_main_server = do_devices.get_cpu(apm_pid_main_server)

        device_001_mem_main_cpdemo = do_devices.get_mem(apm_pid_main_cpdemo)
        device_001_mem_system = do_devices.get_sys_mem(apm_pid_main_cpdemo)
        device_001_mem_att_cpdemo = do_devices.get_mem(apm_pid_pid_att_cpdemo)
        device_001_mem_main_server = do_devices.get_mem(apm_pid_main_server)
        if Secondary_device:
            device_002_cpu_sec_cpdemo = do_devices.get_cpu(apm_pid_sec_cpdemo)
            device_002_cpu_sec_server = do_devices.get_cpu(apm_pid_sec_server)

            device_002_mem_sec_cpdemo = do_devices.get_mem(apm_pid_sec_cpdemo)
            device_002_mem_system = do_devices.get_sys_mem(apm_pid_sec_cpdemo)
            device_002_mem_sec_server = do_devices.get_sys_mem(apm_pid_sec_server)
            measurements = [
                Point("device_001").tag("pid", "main_cpdemo").field("cpu", device_001_cpu_main_cpdemo["appCpuRate"]),
                Point("device_001").tag("pid", "system").field("cpu", device_001_cpu_main_cpdemo["systemCpuRate"]),
                Point("device_001").tag("pid", "att_cpdemo").field("cpu", device_001_cpu_att_cpdemo["appCpuRate"]),
                Point("device_001").tag("pid", "main_server").field("cpu", device_001_cpu_main_server["appCpuRate"]),

                Point("device_001").tag("pid", "main_cpdemo").field("mem", device_001_mem_main_cpdemo),
                Point("device_001").tag("pid", "system").field("mem", device_001_mem_system),
                Point("device_001").tag("pid", "att_cpdemo").field("mem", device_001_mem_att_cpdemo),
                Point("device_001").tag("pid", "main_server").field("mem", device_001_mem_main_server),
                # 分割
                Point("device_002").tag("pid", "sec_cpdemo").field("cpu", device_002_cpu_sec_cpdemo["appCpuRate"]),
                Point("device_002").tag("pid", "system").field("cpu", device_002_cpu_sec_cpdemo["systemCpuRate"]),
                Point("device_002").tag("pid", "sec_server").field("cpu", device_002_cpu_sec_server["appCpuRate"]),

                Point("device_002").tag("pid", "sec_cpdemo").field("mem", device_002_mem_sec_cpdemo),
                Point("device_002").tag("pid", "system").field("mem", device_002_mem_system),
                Point("device_002").tag("pid", "sec_server").field("mem", do_devices.get_mem(apm_pid_sec_server)),
            ]
        else:
            measurements = [
                Point("device_001").tag("pid", "main_cpdemo").field("cpu", device_001_cpu_main_cpdemo["appCpuRate"]),
                Point("device_001").tag("pid", "system").field("cpu", device_001_cpu_main_cpdemo["systemCpuRate"]),
                Point("device_001").tag("pid", "att_cpdemo").field("cpu", device_001_cpu_att_cpdemo["appCpuRate"]),
                Point("device_001").tag("pid", "main_server").field("cpu", device_001_cpu_main_server["appCpuRate"]),

                Point("device_001").tag("pid", "main_cpdemo").field("mem", device_001_mem_main_cpdemo),
                Point("device_001").tag("pid", "system").field("mem", device_001_mem_system),
                Point("device_001").tag("pid", "att_cpdemo").field("mem", device_001_mem_att_cpdemo),
                Point("device_001").tag("pid", "main_server").field("mem", device_001_mem_main_server)
            ]
        do_influxdb.write_mul_data(measurements=measurements)
    except:
        requests.get(url="https://api.day.app/s3KoLFVvWVWdYiiro2gKW/性能采集可能出现问题")
